import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        let that = this;
        let vueDom = this.find("#UserInfo_vue").get(0);
        // 因为此处的用户信息用了双向绑定，为了防止用户编辑了信息却没保存，然后返回首页；这会导致编辑没保存的信息依然会影响全局的变量；
        // 故此处不直接使用globalData来绑定数据，而是复制一份数据，在保存之后再赋值到全局；
        this.vueData = {
            user: funs.deepCopy(globalData.user),
            userAvatar: globalData.user
        };
        let UserInfo_vue = this.UserInfo_vue = new Vue({
            el: vueDom,
            data: that.vueData,
            watch: {
                'user.nick': "checkData",
                'user.address': "checkData",
                'user.company_name': "checkData",
                'user.position': "checkData",

            },
            methods: {
                //限制输入
                checkData: function () {
                    UserInfo_vue.user.mobile = UserInfo_vue.user.mobile.replace(/[^\d]/g, '');
                    UserInfo_vue.user.nick = UserInfo_vue.user.nick.replace(/[^(\u4e00-\u9fa5)]/, '');
                },
            }
        });

    }

    _evt_addAvatar() {
        ////打开上传头像的弹窗
        funs.openModal('modules/user/AvatarUploader.js',function (mod) {
            console.log(mod);
        })
    }

    /**保存个人信息 */
    _evt_saveUserInfo() {
        let that = this;
        let nickname = this.UserInfo_vue.user.nick;
        let gender = this.UserInfo_vue.user.sex;
        let prov = globalData.selPos.prov;
        let city = globalData.selPos.city;
        let addr = this.UserInfo_vue.user.address;
        let com = this.UserInfo_vue.user.company_name;
        let pos = this.UserInfo_vue.user.position;
        let userInfo = {
            nickname: nickname,
            gender: gender,
            prov: prov,
            city: city,
            addr: addr,
            com: com,
            pos: pos
        };
        api.updateUserInfo(userInfo, {
            succ: function () {
                that.setGlobalData();
                that._evt_goback();
            },
            fail: function () {

            }
        });
    }

    // 因为此处的用户信息用了双向绑定，为了防止用户编辑了信息却没保存，然后返回首页；这会导致编辑没保存的信息依然会影响全局的变量；
    // 故此处不直接使用globalData来绑定数据，而是复制一份数据，在保存之后再赋值到全局；
    setGlobalData() {
        let avatar = globalData.user.avatar;
        $.extend(true, globalData.user, this.UserInfo_vue.user);
        globalData.user.avatar = avatar;
    }
}
